package com.example.demo.controller;


import com.example.demo.news.bean.NewsType;
import com.example.demo.news.service.NewsService;
import com.example.demo.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.annotation.Transient;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.validation.Valid;
import java.lang.annotation.Inherited;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;


public abstract class BaseController<T extends BaseService,O extends Object>{


    protected    String rootName = "";



    protected  String DefRequestName = "/Admin";


    @Value("${config.page.size}")
    private int pageSize;



    @Autowired
   protected  T service;




    /**
     * 得到分页列表
     * @param page
     * @param model
     * @return
     */
    @RequestMapping(value = "list")
    public String getList(@RequestParam(name = "page",required = false) String page, Model model){
        PageRequest pageRequest =null;
        if (null==page){
            pageRequest = new PageRequest(0,pageSize);
        }else{
            pageRequest = new PageRequest(Integer.valueOf(page)-1,pageSize);
        }
        model.addAttribute("list",service.getAll(pageRequest));
        return rootName+"list";
    }

    /**
     * 添加
     * @param model
     * @return
     */
    @RequestMapping(value = "add",method = RequestMethod.GET)
    public String addNewEntity(Model model){
        model.addAttribute("entity",getNewInstance());
        return rootName+"add";
    }


    /**
     * 修改
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value ="edit/{id}")
    public String editEntity(@PathVariable int id, Model model){
        model.addAttribute("entity",service.getById(id));
        return rootName+"add";
    }


    /**
     * 保存
     * @param
     * @param result
     * @return
     */
    @RequestMapping(value = "save",method = RequestMethod.POST)
    public String saveEntity(@Valid O entity, BindingResult result){
        if (result.hasErrors())
        {
            return rootName+"add";
        }
        service.save(entity);
        return "redirect:"+DefRequestName+rootName+"list";
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(value = "del/{id}")
    public String delEntity(@PathVariable int id){
        service.delEntity(id);
        return "redirect:"+DefRequestName+rootName+"list";
    }


    public O getNewInstance(){
        ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();
        Class<O> clazz = (Class<O>) pt.getActualTypeArguments()[1];
        try {
            return  clazz.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }
}
